package com.higold.cloud.ui.fragment

import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.higold.cloud.R
import com.higold.cloud.base.BaseFragment
import com.higold.cloud.mvp.contract.MarketListContract
import com.higold.cloud.mvp.model.bean.Common
import com.higold.cloud.mvp.model.bean.marketList.MarketList
import com.higold.cloud.mvp.presenter.MarketListPresenter
import com.higold.cloud.net.exception.ErrorStatus
import com.higold.cloud.showToast
import com.higold.cloud.ui.adapter.MarketAdapter
import kotlinx.android.synthetic.main.fragment_market.*
import java.util.*

/**
 *    author : lxz
 *    date   : 2019/8/15 15:40
 *    desc   :
 */
class MarketFragment : BaseFragment(), MarketListContract.View {

    private val mPresenter by lazy { MarketListPresenter() }

    private var mTitle: String? = null

    private lateinit var mAdapter: MarketAdapter

    companion object {
        fun getInstance(title: String): MarketFragment {
            val fragment = MarketFragment()
            val bundle = Bundle()
            fragment.arguments = bundle
            fragment.mTitle = title
            return fragment
        }
    }

    init {
        mPresenter.attachView(this)
    }

    override fun getLayoutId(): Int = R.layout.fragment_market

    override fun setMarketList(marketList: MarketList) {
        multipleStatusView.showContent()

        rv_market.setHasFixedSize(true)
        rv_market.layoutManager = LinearLayoutManager(activity)

        mAdapter = MarketAdapter(marketList.data)
        mAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_BOTTOM)

        rv_market.adapter = mAdapter
        mAdapter.isFirstOnly(false)//init firstOnly state
    }

    override fun showError(errorMsg: String, errorCode: Int) {
        showToast(errorMsg)
        if (errorCode == ErrorStatus.NETWORK_ERROR) {
            multipleStatusView.showNoNetwork()
        } else {
            multipleStatusView.showError()
        }
    }

    override fun showLoading() {
        multipleStatusView.showLoading()
    }

    override fun dismissLoading() {
    }

    override fun lazyLoad() {
        mPresenter.getMarketList()
    }

    override fun initView() {

    }

    private fun getData(lenth: Int): ArrayList<Common> {
        val list = ArrayList<Common>()
        for (i in 0 until lenth) {
            val common = Common()
            common.size = (i)
            list.add(common)
        }
        return list
    }

}